<?php


namespace app\zhengmaotong\Controller;

use app\zhengmaotong\model\BannerModel;
use cmf\controller\AdminBaseController;

class BannerController extends AdminBaseController
{

    public function index()
    {
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['id', 'like', '%' . $keyword . '%'];
        }

        $list = BannerModel::where($where)
            ->order('list_order', 'desc')
            ->paginate(3);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }
    public function addPost()
    {
        //接收数据
        $data = input('post.');

        $image = cmf_get_image_url($data['image']);
        if(BannerModel::create($data)){
            $this->success('添加成功','Banner/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit()
    {
        $id = input('id');
        $new = BannerModel::where('id', '=', $id)
            ->find();
        $this->assign('news', $new);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');

//        $v=new UserValidate();
//        $res=$v->scene('edit')->check($data);
//        if(!$res){
//            $this->error($v->getError());
//        }
        $model=BannerModel::find($data['id']);
        $model->image=$data['image'];
        if($model->save()){
            $this->success('修改成功','Banner/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model= BannerModel::destroy($id);
        if($model){
            $this->success('删除成功','Banner/index');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function px(){
        $model = new BannerModel;
        parent::listOrders($model);
        $this->success('排序成功');
    }
}

